/*********************************************************
          File Name:sunday.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sun 10 May 2015 08:42:35 AM CST
**********************************************************/
/* a fast string match algorithm */
#include <stdio.h>
#include <string.h>

int in_pattern(const char *pattern, const char ch, int len)
{
    int i= len - 1;

    while(pattern[i] != '\0')
    {
        if (pattern[i] == ch)
          return len - i;
        i--;
    }

    return -1;
}

void sunday_match(const char *src, const char *pattern)
{
    int src_len = strlen(src);
    int ptn_len = strlen(pattern);
    int j = 0, i = 0, pos;

    while (j < src_len)
    {
        i = 0;
        while (pattern[i] != '\0' && src[j+i] == pattern[i])
            i++;

        if (pattern[i] == '\0')
            printf("%s\n",src+j);

        pos = in_pattern(pattern, src[j+ptn_len], ptn_len);
        if(pos != -1)
            j += pos;
        else
            j += (ptn_len + 1);
    }
}

int main(int argc, char **argv)
{
    sunday_match(argv[1],argv[2]);
}
